package com.lvm.app.file;

import com.lvm.app.exception.FFmpegCommandException;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FFmpegUtils {

    public static void main(String[] args) {
        File file = new File("F:\\fileTest\\880\\113M.mp4");
        String ffmpeg = "C:\\Program Files (x86)\\ffmpeg\\bin\\ffmpeg.exe";
        List<String> resFiles = FFmpegUtils.capturePic(file,"D:\\ffmpegTest",10,ffmpeg,null);
        for(String f:resFiles){
                System.out.println(f);
        }
    }

    public static int getVideoTimeLong(String res){
        String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
        Pattern pattern = Pattern.compile(regexDuration);
        Matcher m = pattern.matcher(res);
        if (m.find()) {
            int time = getTimelen(m.group(1));
            return time;
        }
        return 0;
    }
    private static int getTimelen(String timelen) {
        int min = 0;
        String strs[] = timelen.split(":");
        if (strs[0].compareTo("0") > 0) {
            // 秒
            min += Integer.valueOf(strs[0]) * 60 * 60;
        }
        if (strs[1].compareTo("0") > 0) {
            min += Integer.valueOf(strs[1]) * 60;
        }
        if (strs[2].compareTo("0") > 0) {
            min += Math.round(Float.valueOf(strs[2]));
        }
        return min;
    }

    public static List<String> capturePic(File inputFile, String outDir,int number,String ffmpeg,TaskCallBack<Long> callBack2){
        List<String> resFile = new ArrayList<>();
        String ffmpegExePath = ffmpeg;
        String fileName = UUID.randomUUID().toString();
        int initPosition = 1;
        int videoTime =1;
        File outDirFile = new File(outDir);
        if(!outDirFile.exists()){
            outDirFile.mkdirs();
        }
        List<Integer> postions = new ArrayList<>();
        for(int i=0;i<number;i++){
            String outFile = outDir+File.separator+fileName+i+".jpg";
            if(postions.size()>0){
                initPosition = postions.get(i);
            }
            FFmpegCommandBuilder builder = new FFmpegCommandBuilder(ffmpegExePath);
            builder.position(String.valueOf(initPosition)).input(inputFile.getPath()).format(FFmpegCommandFormatEnum.IMAGE)
                    .vframe("1").override().output(outFile+".jpg");
            FFmpegCommand command = builder.build();
            try {
                String result = command.start(null);
                int time = FFmpegUtils.getVideoTimeLong(result);
                resFile.add(outFile);
                if(videoTime==1){
                    postions = FFmpegUtils.random(10,time);
                }
                videoTime = time;
            } catch (FFmpegCommandException e) {
                e.printStackTrace();
            }

        }
        if(callBack2!=null){
            callBack2.callBack(Long.valueOf(videoTime));
        }
        return resFile;
    }

    private static List<Integer> random(int n,int maxNumber){
        ArrayList<Integer> list = new ArrayList<Integer>();
        Random ran = new Random();
        while(list.size() < n){
            int item = ran.nextInt(maxNumber)+1;
            list.add(item);
        }
        return list;
    }
}
